package com.wxtt.android.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fast.dev.frame.glide.GlideLoader;
import com.fast.dev.frame.glide.MulBitmapTransformation;
import com.fast.dev.frame.http.callback.StringHttpCallBack;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.ui.ContentView;
import com.fast.dev.frame.utils.StringUtils;
import com.wxtt.android.R;
import com.wxtt.android.bean.UserBean;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.config.ApiConfig;
import com.wxtt.android.config.SpConfig;
import com.wxtt.android.dialog.TipDialog;

/**
 * 说明：个人中心
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/3/5 11:56
 * <p/>
 * 版本：verson 1.0
 */
@ContentView(R.layout.activity_user_center)
public class UserCenterActivity extends CommonActivity implements TipDialog.TipClickListener {

    @Bind(id = R.id.tv_name)
    private TextView tv_name;
    @Bind(id = R.id.tv_des)
    private TextView tv_des;
    @Bind(id = R.id.tv_sex)
    private TextView tv_sex;
    @Bind(id = R.id.iv_head)
    private ImageView iv_head;

    private UserBean bean;

    @Override
    public int setToolbarTitle() {
        return R.string.user_center;
    }

    @Override
    public void onInit(Bundle bundle) {
        super.onInit(bundle);
        bean = SpConfig.getUserBean();
        if (bean == null) {
            return;
        }

        tv_sex.setText("f".equals(bean.getGender()) ? "女" : "m".equals(bean.getGender()) ? "男" : "未知");
        tv_des.setText(StringUtils.isEmpty(bean.getDescription()) ? getString(R.string.default_description) : bean.getDescription());
        tv_name.setText(bean.getScreen_name());
        GlideLoader.load(this, bean.getProfile_image_url()).centerCrop().transform(new MulBitmapTransformation(this, MulBitmapTransformation.TYPE_CIRCLE)).into(iv_head);
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }

    /**
     * 说明：退出登录
     *
     * @param view
     */
    public void loginOut(View view) {
        showTipDialog(R.string.login_out_tip,this);
    }

    private void loginOutWeibo() {
        ApiConfig.loginOutWeibo(new StringHttpCallBack() {
            @Override
            public void onSuccess(String result) {
                SpConfig.setLoginState(false);
                SpConfig.setUserInfo("");
                finish();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                shortToast(msg);
            }

        });
    }

    @Override
    public boolean onSure() {
        loginOutWeibo();
        return false;
    }

    @Override
    public boolean onCancel() {
        return false;
    }
}
